//
//  LSZAddressBookViewController.m
//  PartyClassCloud
//
//  Created by 梁素珍 on 2018/10/17.
//  Copyright © 2018年 九酷. All rights reserved.
//

#import "LSZAddressBookViewController.h"

@interface LSZAddressBookViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong) UITableView * tableViewAddressBook;
@property(nonatomic,strong) LSZAddressBookManager * addressBookManager;

@end

@implementation LSZAddressBookViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = self.communicationDataMg.name;
     [self.tableViewAddressBook registerClass:[LSZMakePhoneCallTableViewCell class] forCellReuseIdentifier:@"LSZMakePhoneCallTableViewCell"];//拨打电话才用这个
    [self initWIthBarButtonItem];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.0001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.0001;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.addressBookManager.arrayData.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LSZMakePhoneCallTableViewCell * cell = [tableView  dequeueReusableCellWithIdentifier:@"LSZMakePhoneCallTableViewCell" forIndexPath:indexPath];
    if(self.addressBookManager.arrayData.count > indexPath.row ){
        LSZAddressBookMg * mg = nil;
        mg = self.addressBookManager.arrayData[indexPath.row];
        cell.addressBookMg = mg;
        [cell.butPhone tapGesture:^(UIGestureRecognizer *ges) {
            [self initWithPhoneString:mg.contactPhone];
        }];
        [cell.imageViewPhone tapGesture:^(UIGestureRecognizer *ges) {
            [self initWithPhoneString:mg.contactPhone];
        }];
     }
    cell.textLabel.font = FONT_(14);
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
-(void)initWithPhoneString:(NSString *)stringPhone
{
    NSMutableString* str=[[NSMutableString alloc] initWithFormat:@"tel:%@",stringPhone];
    UIWebView * callWebview = [[UIWebView alloc] init];
    [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
    [self.view addSubview:callWebview];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(self.addressBookManager.arrayData.count > indexPath.row ){
        LSZAddressBookMg * mg = nil;
        mg = self.addressBookManager.arrayData[indexPath.row];
        [self initWithPhoneString:mg.contactPhone];
    }
}
-(void)initWIthBarButtonItem
{
//    UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"添加"] style:UIBarButtonItemStylePlain target:self action:@selector(clickReleaseItem)];
//    self.navigationItem.rightBarButtonItem = rightBarItem;
    [LSZRequestDicData initGetAddressBookData:self.communicationDataMg.id_id :^(BOOL isOK, NSString * string, LSZAddressBookManager * addressBookManager) {
        if (isOK == YES) {
            self.addressBookManager = addressBookManager;
            [self.tableViewAddressBook reloadData];
        }else{
            if ([string isEqualToString:@"重复登录"]) {
                [LSZPublicClassDataToll RepeatLoginInterfaceJump:self.view fromVC:@"LSZStudyPlanViewController"];
            }else{
                [LSZPublicClassDataToll initWithAlertKuangView:self.view string:string];
            }
        }
    }];
}
-(void)clickReleaseItem
{
    LSZConferenceClassViewController * VC = [[LSZConferenceClassViewController alloc]init];
    [self.navigationController pushViewController:VC animated:YES];
}
-(UITableView *)tableViewAddressBook
{
    if (_tableViewAddressBook == nil) {
        _tableViewAddressBook = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width , [[UIScreen mainScreen] bounds].size.height) style:UITableViewStylePlain];
        _tableViewAddressBook.delegate = self;
        _tableViewAddressBook.dataSource = self;
        _tableViewAddressBook.estimatedRowHeight=0;
        _tableViewAddressBook.estimatedSectionFooterHeight =0;
        _tableViewAddressBook.estimatedSectionHeaderHeight =0;
        [_tableViewAddressBook setSeparatorInset:UIEdgeInsetsMake(0, 20, 0, 20)];
        [self.view addSubview:_tableViewAddressBook];
    }
    return _tableViewAddressBook;
}

@end
